//
//  LZSearchResultView.m
//  laziz_Consumer
//
//  Created by XMac on 16/12/16.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZSearchResultView.h"
#import "SKSTableView.h"
#import "Masonry.h"
#import "SKSTableViewCell.h"
#import "LZSearchHistoryTableViewCell.h"
#import "LZSearchHistoryUserdefault.h"


extern NSString *bound;

@interface LZSearchResultView ()<SKSTableViewDelegate>

@property(nonatomic , strong) NSArray *contents;

@property(nonatomic , strong) NSArray *titles;

@property(nonatomic , strong) NSArray *images;

@property(nonatomic , strong) SKSTableView *sksTableView;

@end

@implementation LZSearchResultView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initTestData];
        
        [self configUI];
        
        [self registerCell];
    }
    return self;
}

-(void)dealloc{
    DLog(@"search result dealloc");
}

#pragma
#pragma mark private method

-(void)initTestData{
    _contents = @[];
    
    _titles = @[LZLocalizedString(@"restaurant_video", nil), LZLocalizedString(@"restaurant_cuisine", nil), LZLocalizedString(@"mine_restaurants", nil), LZLocalizedString(@"tag", nil)];
    _images = @[@"ic_video",@"ic_cuisine",@"ic_restaurant",@"ic_tags"];
}

-(void)registerCell{
    [_sksTableView registerNib:[UINib nibWithNibName:@"SKSTableViewCell" bundle:nil] forCellReuseIdentifier:@"SKSTableViewCell"];
    
    [_sksTableView registerNib:[UINib nibWithNibName:@"LZSearchHistoryTableViewCell" bundle:nil] forCellReuseIdentifier:@"LZSearchHistoryTableViewCell"];
}

-(void)configUI{
    [self setBackgroundColor:[UIColor clearColor]];
    
    _sksTableView = [[SKSTableView alloc]init];
    
    _sksTableView.backgroundColor = [UIColor clearColor];
    
    _sksTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.sksTableView.SKSTableViewDelegate = self;

    [self addSubview:self.sksTableView];
    
    [self.sksTableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 0, 49, 0));
    }];
}

#pragma
#pragma mark public method

-(void)refreshDatas:(NSArray *)xDatas{
    self.contents = xDatas;
    [self.sksTableView refreshDataWithScrollingToIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
}


#pragma
#pragma mark SKSTableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.contents.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.contents[section] count];
}

-(NSInteger)tableView:(SKSTableView *)tableView numberOfSubRowsAtIndexPath:(NSIndexPath *)indexPath{
    return  [self.contents[indexPath.section][indexPath.row] count] - 1;
}

- (BOOL)tableView:(SKSTableView *)tableView shouldExpandSubRowsOfCellAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SKSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SKSTableViewCell"];
    
    int firstElement = [self.contents[indexPath.section][indexPath.row][0] intValue];
    
    [cell changeTitle:_titles[firstElement]];

    [cell changeTitleImage:[UIImage imageNamed:_images[firstElement]]];
    
    return cell;
}

//todo 需要设定一个child cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubRowAtIndexPath:(NSIndexPath *)indexPath
{
    LZSearchHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LZSearchHistoryTableViewCell"];
    
    NSString *content = self.contents[indexPath.section][indexPath.row][indexPath.subRow];
    
    [cell changeTitle:[content componentsSeparatedByString:bound][0]];

    
    return cell;
}
- (CGFloat)tableView:(SKSTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f;
}

- (CGFloat)tableView:(SKSTableView *)tableView heightForSubRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)tableView:(SKSTableView *)tableView didSelectSubRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点击添加到userdefault
    NSMutableArray *historyArray = [LZSearchHistoryUserdefault sharedInstance].searchHistoryResults.mutableCopy;
    
    [historyArray addObject:self.contents[indexPath.section][indexPath.row][indexPath.subRow]];
    
    [LZSearchHistoryUserdefault sharedInstance].searchHistoryResults = historyArray;
    
    //跳转
    if (_selectBlock) {
        NSString *content = self.contents[indexPath.section][indexPath.row][indexPath.subRow];
        NSArray *resultArray = [content componentsSeparatedByString:bound];
        NSString *resultMessage = @"";
        NSString *resultType = @"";
        NSString *resultId = @"";
        if (resultArray.count>0) {
            resultMessage = resultArray[0];
        }
        if (resultArray.count>1) {
            resultType = resultArray[1];
        }
        if (resultArray.count>2) {
            resultId = resultArray[2];
        }
        _selectBlock(resultMessage,resultType,resultId);
    }
}


@end
